<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button @click="dialogVisble = true" type="primary" size="mini">添加分类</el-button>
      </div>
      <!-- 表格渲染文章分类的数据 -->
        <el-table
            border
            :data="list"
            style="width: 100%">
        <el-table-column
            type="index"
            label="序号"
            width="50">
        </el-table-column>
        <el-table-column
            prop="cate_name"
            label="分类名称"
            >
        </el-table-column>
        <el-table-column
            prop="cate_alias"
            label="分类别名"
            >
        </el-table-column>
        <el-table-column 
            label="操作">
            <template #default={row}>
              <el-button @click="getArtCateById(row.id)" type="primary" >修改</el-button>
              <el-button @click="delArtCate(row.id)" type="danger">删除</el-button>
            </template>
        </el-table-column>
        </el-table>
    </el-card>
    <!-- 新增 -->
    <el-dialog @close="resetForm" title="添加文章分类" :visible.sync="dialogVisble" width="30%">
        <!-- 添加的表单 -->
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px">
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="ruleForm.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="ruleForm.cate_alias" ></el-input>
          </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisble = false">取消</el-button>
          <el-button @click="addArtCate" type="primary">确定</el-button>
        </span>
      </el-dialog >

      <!-- 修改 -->
      <el-dialog  title="修改文章分类" :visible.sync="uDidlogVisible" width="30%">
        <!-- 修改文章分类的弹层 -->
        <el-form :model="uRuleForm" :rules="rules" ref="uRuleForm" label-width="80px">
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="uRuleForm.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="uRuleForm.cate_alias" ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="uDidlogVisible = false">取消</el-button>
          <el-button @click="updateArtCate" type="primary">确定</el-button>
        </span>
      </el-dialog >
    </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data(){
    return {
        uDidlogVisible:false, //控制修改 弹层显示和隐藏
        dialogVisble:false,  //控制添加弹层显示和隐藏
        list:[], //保存文件分类的列表
        ruleForm:{
          cate_name:'',
          cate_alias:''
        },
        uRuleForm:{
          cate_name:'',
          cate_alias:''
        },
        rules:{
          cate_name:[
              { required: true, message: '请输入分类名称', trigger: 'blur' },
              { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
          ],
          cate_alias:[
              { required: true, message: '请输入分类别名', trigger: 'blur' },
              { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
          ]
        }
    }
  },
  created(){
    this.getArtCateList()
  },
  methods:{
    //添加表单
    addArtCate(){
      this.$refs.ruleForm.validate(async(valid)=>{
        if(!valid) return //校验失败
        const {data:res} = await this.$http.post('/my/cate/add',this.ruleForm)
        if(res.code !==0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.getArtCateList()
        this.dialogVisble = false
      })
    },
    async getArtCateList(){
        const {data:res} = await this.$http.get('/my/cate/list')
        if(res.code !== 0) return
        this.list = res.data
    },
    //重置表单
    resetForm(){
      this.$refs.ruleForm.resetFields()
    },
    //  获取文章分类的详情数据
    async getArtCateById(id){
      const {data:res} = await this.$http.get('/my/cate/info',{params:{id}})
      if(res.code ===0){
        console.log('res.data',res);
        this.uRuleForm=res.data
        this.uDidlogVisible = true
      }
    },
    //修改表单
    updateArtCate(){
      this.$refs.uRuleForm.validate(async(valid)=>{
        if(!valid) return //校验失败
        const {data:res} = await this.$http.put('/my/cate/info',this.uRuleForm)
        if(res.code !==0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.getArtCateList()
        this.uDidlogVisible = false
      })
    },
    //删除表单
    delArtCate(id){
      this.$confirm('此操作将永久删除该文章分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const {data:res} = await this.$http.delete('/my/cate/del',{params:{id}})
        if(res.code !==0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.getArtCateList() //重新渲染列表
      }).catch(() => {

      });
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>